package com.kuang.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {


    /**
     * 登录
     * @param username
     * @param password
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/user/login")
    //@ResponseBody    只能返回对象,不走解析器了就  要去掉
    public String login(@RequestParam("username") String username, @RequestParam("password") String password, Model model,
                        HttpSession session) {

        if (!StringUtils.equals("root", username)) {
            //告诉用户 , 你登录失败了 , Model传值
            model.addAttribute("msg", "账号不存在");
            return "index";
        } else {
            if (StringUtils.equals("888", password)) {
                //return "dashboard";
                session.setAttribute("loginUser", username);
                return "redirect:/main.html";
            } else {
                //告诉用户 , 你登录失败了 , Model传值
                model.addAttribute("msg", "密码错误");
                return "index";
            }
        }

    }


    /**
     * 注销
     * @param session
     * @return
     */
    @RequestMapping("/user/logout")
    public String logout(HttpSession session){
        session.invalidate();

        return  "redirect:/index.html";
    }

}
